package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;


@Component(value = "appWxModelConfigDao")
public class AppWxModelConfigDao {
	


	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppWxModelConfig queryWithKeys(String dxModelId, int modelLineNum) throws SQLException {
		AppWxModelConfig appWxModelConfig = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM  wx_model_detail WHERE dx_model_id=? and model_line_num=?");
			ps.setString(1, dxModelId);
			ps.setInt(2, modelLineNum);
			rs = ps.executeQuery();
			if (rs.next()) {
				appWxModelConfig = new AppWxModelConfig();
				appWxModelConfig.setDxModelId(rs.getString("dx_model_id"));
				appWxModelConfig.setModelLineNum(rs.getInt("model_line_num"));
				appWxModelConfig.setAppId(rs.getString("app_id"));
				appWxModelConfig.setLineName(rs.getString("line_name"));
				appWxModelConfig.setLineStaticPart(rs.getString("line_static_part"));
				appWxModelConfig.setLineChangePart(rs.getString("line_change_part"));
			}
		} catch (SQLException e) {
			throw new SQLException("query wx_model_detail WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appWxModelConfig;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppWxModelConfig appWxModelConfig) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into wx_model_detail(dx_model_id  , model_line_num  , app_id  , line_name , line_static_part  , line_change_part) values(? , ? , ? , ? , ? , ? )";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appWxModelConfig.getDxModelId());
			ps.setInt(2, appWxModelConfig.getModelLineNum());
			ps.setString(3, appWxModelConfig.getAppId());
			ps.setString(4, appWxModelConfig.getLineName());
			ps.setString(5, appWxModelConfig.getLineStaticPart());
			ps.setString(6, appWxModelConfig.getLineChangePart());
			
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert wx_model_detail is Wrong!" + e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppWxModelConfig appWxModelConfig) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update wx_model_detail set app_id=? , line_name=? , line_static_part=? , line_change_part=? where dx_model_id=? and model_line_num=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appWxModelConfig.getAppId());
			ps.setString(2, appWxModelConfig.getLineName());
			ps.setString(3, appWxModelConfig.getLineStaticPart());
			ps.setString(4, appWxModelConfig.getLineChangePart());
			ps.setString(5, appWxModelConfig.getDxModelId());
			ps.setInt(6, appWxModelConfig.getModelLineNum());
			
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("update wx_model_detail WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String dxModelId, int modelLineNum) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from wx_model_detail where dx_model_id=? and model_line_num=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, dxModelId);
			ps.setInt(2, modelLineNum);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("delete wx_model_detail WithKeys is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}


	
}
